#include <stm32f10x_rcc.h>
#include <stm32f10x_gpio.h>

#include "app.h"


u8 key_flag = 0x00;
//#define KEY0_PRESS  (0x01)
//#define KEY1_PRESS  (0x02)
//#define KEYUP_PRESS (0x04)

#define KEY_STATUS_PRESS   (1)
#define KEY_STATUS_RELEASE (0)

#define KEY0_STATUS (GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_4)?(KEY_STATUS_RELEASE):(KEY_STATUS_PRESS)) //1:KEY_RELEASE,0:press
#define KEY1_STATUS (GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_3)?(KEY_STATUS_RELEASE):(KEY_STATUS_PRESS))
#define KEYUP_STATUS (GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)?(KEY_STATUS_PRESS):(KEY_STATUS_RELEASE))    //1:press 0:release

int app_key_init(void)
{
	GPIO_InitTypeDef key_gpio = {0};
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOE,ENABLE);
	key_gpio.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4;
	key_gpio.GPIO_Mode = GPIO_Mode_IPU;
	key_gpio.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOE,&key_gpio);
	
	key_gpio.GPIO_Pin = GPIO_Pin_0;
	key_gpio.GPIO_Mode = GPIO_Mode_IPD;
	key_gpio.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&key_gpio);
	
	return 0;
}



u8 app_key_scan(void)
{
	if(0x00 == (key_flag&KEYUP_PRESS) && (KEYUP_STATUS == KEY_STATUS_PRESS)){
		delay_ms(20);
		if(KEYUP_STATUS == KEY_STATUS_PRESS){
			key_flag |= KEYUP_PRESS;
		}
	}else if((key_flag&KEYUP_PRESS) && (KEYUP_STATUS == KEY_STATUS_RELEASE)){
		delay_ms(20);
		if(KEYUP_STATUS == KEY_STATUS_RELEASE){
			key_flag &= ~KEYUP_PRESS;
		}
	}
	
	return key_flag;
}
